﻿using System;
using System.Web.Mvc;
using cludg.Web.Core.Controllers;

namespace cludg.Web.Core.RenderHelpers
{
	public static class ImageRenderHelper
	{
		public static MvcHtmlString RenderImage(this HtmlHelper html, string imageId, string imageClass = null)
		{
			if (string.IsNullOrEmpty(imageId))
				return MvcHtmlString.Create("");

			// Get view register from controller
			var controller = html.ViewContext.Controller as BaseController;
			if (controller == null)
			{
				throw new ApplicationException("RenderImage helper can be user only with BaseController objects.");
			}

			return MvcHtmlString.Create(string.Format("<img src=\"{0}\" alt=\"Image\"{1}/>", controller.ImageService.GetUrl(imageId),
				(!String.IsNullOrEmpty(imageClass) ? String.Format(" class=\"{0}\"", imageClass) : "")));
		}

		public static MvcHtmlString ImageUrl(this HtmlHelper html, string imageId)
		{
			// Get view register from controller
			var controller = html.ViewContext.Controller as BaseController;
			if (controller == null)
			{
				throw new ApplicationException("RenderImage helper can be user only with BaseController objects.");
			}

			return MvcHtmlString.Create(controller.ImageService.GetUrl(imageId));
		}
	}
}
